//
//  ConfirmFeeVC.m
//  test
//
//  Created by 李彦宏 on 2018/3/8.
//  Copyright © 2018年 朱圳. All rights reserved.
//已同意/未支付

#import "ConfirmFeeVC.h"
#import "UnConfHead.h"//section==0头部
#import "JWGRAgreeHead.h"//section==1头部
#import "FeeDetailCell.h"//内容详情cell
#import "JWGRUsermodel.h"
#import "JWUserManager.h"
#import "JWGROtherFeemodel.h"
#import "Utility.h"
#import "KongDataView.h"
#define FeeDetailCellID1 @"FeeDetailCell"
@interface ConfirmFeeVC ()<UITableViewDataSource,UITableViewDelegate,UIGestureRecognizerDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *selectArry;//开合Arry
@property (nonatomic, strong) JWGRUsermodel *Gmodel;
@property (nonatomic, strong) JWGROtherFeemodel *model;
@property (nonatomic, strong) NSMutableArray *contArry;//内容
@end

@implementation ConfirmFeeVC
//UItableView初始化
- (void)setupTable
{
    UITableView *tableView=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.backgroundColor=[UIColor whiteColor];
    tableView.showsHorizontalScrollIndicator=NO;
    tableView.showsVerticalScrollIndicator=NO;
    //自适应cell
    self.tableView.estimatedRowHeight = 120;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.dataSource=self;
    tableView.delegate=self;
    tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableView];
    self.tableView=tableView;
    
    [tableView registerNib:[UINib nibWithNibName:@"FeeDetailCell" bundle:nil] forCellReuseIdentifier:FeeDetailCellID1];
    self.tableView=tableView;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [EasyShowOptions sharedEasyShowOptions].lodingShowType =  LodingShowTypeTurnAround ;
    [EasyShowOptions sharedEasyShowOptions].textStatusType =  ShowTextStatusTypeStatusBar ;
    [EasyShowLodingView showLodingText:@""];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self setdata];
        [EasyShowLodingView hidenLoding];
    });
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTable];
    self.view.backgroundColor=[UIColor whiteColor];
     self.Gmodel=[JWUserManager Gme];

    self.title=@"已同意";
}
#pragma -mark -----------------------------tableview datasource and delegate---------------------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   return self.contArry.count+1;;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([_selectArry[section] isEqualToString:@"1"]) {
        return 1;
    }
    return 0;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FeeDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:FeeDetailCellID1];
 cell.selectionStyle=UITableViewCellSelectionStyleNone;
    self.model=_contArry[indexPath.section-1];
    cell.DetailTextView.text=self.model.discrabe;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}


#pragma -mark 头部的内容
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if(section==0){
        UnConfHead *head1=[[UnConfHead alloc]init];
        head1.caozuoLb.text=@"操作";
        return head1;
    }else{
        if(self.Gmodel.ID==nil){
            self.Gmodel.ID=self.Gmodel.gid;
        }
        JWGRAgreeHead *head=[[JWGRAgreeHead alloc]init];
        [head setPayBtnBlock:^(NSString *str) {
            self.model=self.contArry[section-1];
                NSDictionary *dic=@{@"jwid" : JWid,
                                    @"jwpassword" : JWpassword,
                                    @"gid":self.Gmodel.ID,
                                    @"state":str,
                                    @"cid":self.model.ID,
                                    };
        //        NSLog(@"-->>dic--%@", dic);
                
                [[AFNetworkTool shareManager]postJSONWithUrl:JWteacher_GROthDealFee parmas:dic successData:^(id json) {
                    
                   // NSLog(@"--json--%@", json);
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [MBProgressHUD showSuccess:@"发送成功"];
                        [self.contArry removeAllObjects];
                        [self setdata];
                        
                        
                        
                    });
                } failure:^(NSError *error) {
                    NSLog(@"--error--%@", error);
                }];
            }];
         self.model=_contArry[section-1];
         NSString *time=[Utility getDateStringWithTimeStr:self.model.time];
        head.sum.text=self.model.money;
        head.data.text=time;
        
        [head.cont addTarget:self action:@selector(detailsBtn:) forControlEvents:UIControlEventTouchUpInside];
        head.cont.tag=1000+section;
        head.cont.showsTouchWhenHighlighted=YES;
        return head;
    }
}


#pragma -mark —————————点击详情响应————————————————————
-(void)detailsBtn:(UIButton *)button{
    if ([_selectArry[button.tag - 1000] isEqualToString:@"0"]) {
        
        [_selectArry replaceObjectAtIndex:button.tag - 1000 withObject:@"1"];
        [_tableView reloadSections:[NSIndexSet indexSetWithIndex:button.tag - 1000] withRowAnimation:UITableViewRowAnimationFade];
    }
    else
    {
        //如果当前点击的section是展开的,那么点击后就需要把它缩回,使_selectedArray对应的值为0,这样当前section返回cell的个数变成0,然后刷新这个section就行了
        
        [_selectArry replaceObjectAtIndex:button.tag - 1000 withObject:@"0"];
        [_tableView reloadSections:[NSIndexSet indexSetWithIndex:button.tag - 1000] withRowAnimation:UITableViewRowAnimationFade];
    }
    
}
#pragma -mark ———————————————已同意数据————————————————————
-(void)setdata{
    if(self.Gmodel.ID==nil){
        self.Gmodel.ID=self.Gmodel.gid;
    }
    NSDictionary *dic=@{
                        @"jwid" : JWid,
                        @"jwpassword" : JWpassword,
                        @"gid":self.Gmodel.ID,
                        @"state":@"1",
                        };

    [[AFNetworkTool shareManager]postJSONWithUrl:JWteacher_GROthFeelist parmas:dic successData:^(id json) {
        [self.contArry removeAllObjects];
        NSArray *arry=json[@"list"];
        for(int i=0;i<arry.count;i++){
            self.model=[JWGROtherFeemodel yy_modelWithJSON:arry[i]];
            [self.contArry addObject:self.model];
        }
        for(int a=0;a<arry.count+1;a++){
            [self.selectArry addObject:@"0"];
        }
        __weak typeof(self) weakslf = self;
        dispatch_async(dispatch_get_main_queue(), ^{
            if(self.contArry.count==0){
                [self.tableView removeFromSuperview];
                KongDataView *konview=[KongDataView NilDataview];
                konview.frame=self.view.frame;
                [self.view addSubview:konview];
                
            }
           
            
            [weakslf.tableView reloadData];
            
        });
        
    } failure:^(NSError *error) {
        NSLog(@"--error--%@", error);
    }];
    
}

-(NSMutableArray *)selectArry{
    if (!_selectArry) {
        _selectArry = [[NSMutableArray alloc]init];
    }
    return _selectArry;
}
-(NSMutableArray *)contArry{
    if(_contArry==nil){
        _contArry=[[NSMutableArray alloc]init];
    }
    return _contArry;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
